mui.init()
var detail = new Vue({
	el: '.mui-content',
	data: {
		data: [],
		imgurl: '',
		cls: [],
		state: "",
	},
	updated: function() {
		$(".slide").height($(".content").height())
	}
})

$(document).on("tap", ".purchase", function() {
	if($(this).attr("href") == '') {
		mui.toast("该商品路径不对!!")
		return
	}
	/*common.open("microShop.html","microShop.html",$(this).attr("href"))*/
	mui.openWindow({
		id: "microShop.html",
		url: 'microShop.html',
		extras: {
			url: $(this).attr("href")
		}
	});
})
$(document).on("tap", ".bookshelf", function() {
	var self = $(this)
	common.ajax("student/myShelf/addBook", {
		uid: common.uid,
		token: common.token,
		bookUid: $(this).attr("uid")
	}, function(data) {
		if(data.code == 200) {
			self.text("已添加")
			self.addClass("truebookshelf")
			mui.toast("添加成功!!!")
			/*var list = plus.webview.currentWebview("../reading/joinreading.html").opener();
			var recommen = plus.webview.currentWebview("../stacks/Recommended.html").opener();
			mui.fire(recommen,'Recommended')
			mui.fire(list, 'joinreadingggg');*/
			var load = plus.webview.getWebviewById("bookshelf.html");
			mui.fire(load, "index");
			var go = plus.webview.getWebviewById("../stacks/Recommended.html")
			mui.fire(go, "refresh")
		} else {
			mui.toast(data.message)
		}
	})
})

if(localStorage.getItem("state") == "true") {
	detail.state = true
} else {
	detail.state = false
}

mui('.mui-slider').slider().setStopped(true)
var temp = 120
window.onload = function() {
	mui('#offCanvasContentScroll').scroll();
}
var scroll = mui('.mui-scroll-wrapper').scroll({
	startY: 0, //初始化时滚动至y
	indicators: false, //是否显示滚动条
	bounce: false
});

document.querySelector('#offCanvasContentScroll').addEventListener('scroll', function(e) {
	if(scroll.y >= -temp * 2) {
		$(".content").css("top", scroll.y * 0.5)
	} else {
		$(".content").css("top", "-" + temp + "px")
	}
})

function parent(id) {

	////	"student/bookCity" + id +""
	//'student/bookCity/'+id
	common.ajax('student/bookCity/' + id, {
		token: common.token,
		uid: common.uid,
		bookUid: id
	}, function(data) {
		console.log(JSON.stringify(data.data))
		if(data.code == 200) {

			detail.data = data.data
			joinissue()
			$("body").show()
		} else {
			mui.toast(data.message)
		}
	})
}

function joinissue() {
	var addbook = JSON.parse(localStorage.getItem('books')) || []
	$.each(addbook, function(index, id) {
		if(id.bookid == detail.data.id) {
			$(".joinissue").text("已添加到计划")
			$(".joinissue").addClass("truebookshelf")
			$(".joinissue").removeClass("joinissue")
		}
	})
	$(document).on("tap", ".joinissue", function() {
		var addbook = JSON.parse(localStorage.getItem('books')) || []
		var obj = {}
		var temp = []
		obj.bookid = detail.data.id
		obj.booksrc = detail.data.bookPic
		obj.bookname = detail.data.bookName

		if(addbook == "") {
			addbook.push(obj)
		} else {
			for(var i = 0; i < addbook.length; i++) {
				temp.push(addbook[i].bookid)
			}
			if(temp.indexOf(obj.bookid) == -1) {
				addbook.push(obj)
			}
		}
		$(this).text("已添加到计划")
		$(this).addClass("truebookshelf")
		$(this).removeClass("joinissue")
		localStorage.setItem("books", JSON.stringify(addbook))
		var list = plus.webview.currentWebview("joinreading.html").opener();
		mui.fire(list, 'joinreadingggg');
	})
}